package com.system.main;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.system.main.impl.WindowsSystem;

@Component("mysystem_server")
@Lazy(false)
public class SystemServer {
	
	String unknownPath = "/system/images/unknown.ico";
	
	/**
	 * 文件类型图标
	 * key是 type value是图标路径
	 */
	private Map<String,String> iconPaths = new HashMap<String,String>();
	
	private static SystemServer instance;
	
	private static MySystem system;
	
	public void init() throws Exception{
		initSystemType();
		initIconPaths();
		instance=this;
	}
	
	/**
	 * 初始化系统类型
	 */
	private void initSystemType() {
		String osname = System.getProperty("os.name").toLowerCase();
		if(osname.indexOf("windows")!=-1){
			system = new WindowsSystem();
		}else{
			system= null;
		}
		
	}
	
	/**
	 * 初始化文件图标
	 * @throws Exception 
	 */
	private void initIconPaths() throws Exception {
			File in = new File("src/com/config/iconPath.xml");
			SAXReader reader = new SAXReader();              
			Document   document = reader.read(in);  
			Element root = document.getRootElement();
			List<Element> elements = root.elements();
			for(Element e :elements){
				String type= e.attributeValue("type");
				String path = e.attributeValue("src");
				iconPaths.put(type, path);
			}
	}
	
	public String getIconPath(String type){
		 String path = iconPaths.get(type);
		 if(path==null){
			 path = unknownPath;
		 }
		 return path;
		 
	}
	/**
	 * 获取当前系统
	 * @return
	 */
	public static MySystem getCurrentSystem(){
		return system;
	}
	
	public static SystemServer getInstance(){
		return instance;
	}
	
	
}

